#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cexapi
import datetime
import time
import re

credentialFile = open('credentials.txt', 'r+')

for line in credentialFile:
        resultLine = re.split(';', line)  
        if resultLine[0]=='hannesh':
            apiUser=resultLine[0]
            apiCred1=resultLine[1]
            apiCred2=resultLine[2]      

demo = cexapi.api(apiUser, apiCred1, apiCred2)

current_orders = demo.current_orders()

sortedCurrOrders = sorted(current_orders, key=lambda order: order['price'], reverse=True) 

for order in sortedCurrOrders:
    print order['type'],"price=",order['price'], "amount=",order['amount'], "pending=",order['pending'], "id=",order['id']
    #print order
    
sell_min=-1
buy_max=-1

ghs_smallBoundaries=0.0001
outer_sell_order=0

if current_orders:
    for order in sortedCurrOrders:
        if order['type']=='sell':
            sell_min=float(order['price'])       
            sell_min_order=order
            if float(order['amount'])>ghs_smallBoundaries and not outer_sell_order:
                outer_sell_order=order
        elif order['type']=='buy':
            if buy_max<0:             
                buy_max=float(order['price'])        
                buy_max_order=order
            if float(order['amount'])>ghs_smallBoundaries:
                outer_buy_order=order
            


    print""     
    print sell_min_order['type'],"price=",sell_min_order['price'], "amount=",sell_min_order['amount'], "pending=",sell_min_order['pending'], "id=",sell_min_order['id']
    print buy_max_order['type'],"price=",buy_max_order['price'], "amount=",buy_max_order['amount'], "pending=",buy_max_order['pending'], "id=",buy_max_order['id']
    diff=float(sell_min_order['price'])-float(buy_max_order['price'])
    print "diff=",diff*100000000
    
    #print "outer Sell",outer_sell_order
    #print "outer_buy",outer_buy_order
    
else:
    print "No orders found"

   
